package cycling.taxi.com.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cycling.taxi.com.R;
import cycling.taxi.com.manager.FragmentFactory;
import cycling.taxi.com.ui.activity.AutofixActivity;
import cycling.taxi.com.ui.activity.RepairActivty;
import cycling.taxi.com.utils.ToastUtil;
import github.chenupt.springindicator.SpringIndicator;

/**
 * Created by Administrator on 2017/4/20.
 */
public class ServeFrgt extends Fragment implements View.OnClickListener {
  //  private SpringIndicator mFrgtServiceIndicator;
    String [] title={"贴士","检修","报修","自主检修"};
    //String [] title={"贴士"};
    private ViewPager mFrgtServiceViewpager;
    private TextView  call,autofix,repair,tips;
    //////////
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdpter;
    private List<Fragment> mFragments = new ArrayList<>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(inflater.getContext()).inflate(R.layout.frgt_service, null);
//        initUI(view);
//        initData();
//        initListener();
        initViews(view);//初始化控件
        initEvent();//监听逻辑事件
        initViewPages();//初始化viewpager
        return view;
    }
    private void initViews(View view){
        mViewPager=(ViewPager)view.findViewById(R.id.frgt_service_viewpager);
        call = (TextView) view.findViewById(R.id.call);
        autofix = (TextView) view.findViewById(R.id.autofix);
        repair=(TextView)view.findViewById(R.id.repair);
        tips=(TextView)view.findViewById(R.id.tips);
    }
    private void initEvent() {
        //设置监听器
        call.setOnClickListener(this);
        autofix.setOnClickListener(this);
        repair.setOnClickListener(this);
        tips.setOnClickListener(this);
    }
    private void initViewPages() {
       mViewPager.setAdapter(new MyViewPagerAdapter(getChildFragmentManager()));

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tips:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.repair:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.call:
                mViewPager.setCurrentItem(2);
                break;
            case R.id.autofix:
                mViewPager.setCurrentItem(3);
                break;
            default:
                break;

        }


    }
    public class MyViewPagerAdapter extends FragmentPagerAdapter{


        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return title==null?0:title.length;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = FragmentFactory.getInstance().getFragment(position);
            return fragment;
        }
    }
    @Override
    public void onDestroy() {

        super.onDestroy();
    }

   /* private void initUI(View view) {
        mFrgtServiceViewpager = (ViewPager) view.findViewById(R.id.frgt_service_viewpager);
        mFrgtServiceIndicator = (SpringIndicator) view.findViewById(R.id.frgt_service_indicator);
        call = (TextView) view.findViewById(R.id.call);
        autofix = (TextView) view.findViewById(R.id.autofix);
        repair=(TextView)view.findViewById(R.id.repair);

    }

    private void initListener() {
        mFrgtServiceIndicator.setOnPageChangeListener(new PagerChangeListener());
        call.setOnClickListener(this);
        autofix.setOnClickListener(this);
        repair.setOnClickListener(this);
    }

    private void initData() {
        //给Viewpager设置适配器并跟Indicator绑定
        mFrgtServiceViewpager.setAdapter(new MyViewPagerAdapter(getChildFragmentManager()));
        mFrgtServiceIndicator.setViewPager(mFrgtServiceViewpager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.call:
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                 builder.setMessage("确认要拨打客服电话:18378307553？");
                builder.setTitle("提示");
                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                 @Override
                public void onClick(DialogInterface dialog, int which) {
                     Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+"18378307553"));
                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     startActivity(intent);
                     }
                 });
                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                 @Override
                   public void onClick(DialogInterface dialog, int which) {
                     dialog.dismiss();
                 }
                  });
                  builder.create().show();
                break;
            case R.id.autofix:
                Intent auto=new Intent(getActivity(),AutofixActivity.class);
                startActivity(auto);
                break;
            case R.id.repair:
                Intent repair=new Intent(getActivity(),RepairActivty.class);
                startActivity(repair);
                break;
        }
    }

    public class PagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    public class MyViewPagerAdapter extends FragmentPagerAdapter{


        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return title==null?0:title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (title != null) {
                return title[position];
            }
            return super.getPageTitle(position);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = FragmentFactory.getInstance().getFragment(position);
            return fragment;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }*/
}
